﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTerm.MsSql.DbProvider;

namespace eTerm.Platform.Web
{
    public partial class CabinSetting : BasePage 
    {
        protected override string PageCode
        {
            get
            {
                return @"CARBINCODE";
            }
        }

        protected override void InitEasyUI()
        {
            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnQuery.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-search'  
}});  
", btnQuery.ClientID), true);

            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnNew.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-add'  
}});  
", btnNew.ClientID), true);

            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, dlCompany.ClientID, string.Format(@"$('#{0}').combobox();  
", dlCompany.ClientID), true);

            btnNew.OnClientClick = string.Format(@" 
$.jBox(""iframe:CabinEditor.aspx?ID={0}"", {{
    title: ""舱位信息"",
    width: 450,
    height:270,
    buttons: {{ }},
    closed: function () {{ 
        $('#{1}').click();
    }} /* 窗口关闭后执行的函数 */
}});
return false;", 0,btnQuery.ClientID);


        }

        protected override void InitPageLoad()
        {
            dlCompany.DataSource = new List<PnrCabin>() { new PnrCabin() { AirWayCode = string.Empty } }.Union(PnrCabin.All().Select(l => new PnrCabin() { AirWayCode = l.AirWayCode }).Distinct().ToList());
            dlCompany.DataBind();
            QueryData();
        }

        private void QueryData() {
            var sql = PnrCabin.All();

            if (!string.IsNullOrEmpty(dlCompany.SelectedValue))
                sql = sql.Where(l => l.AirWayCode == dlCompany.SelectedValue);

            if (!string.IsNullOrEmpty(tbCabinCode.Text.Trim()))
                sql = sql.Where(l => l.CabinCode.Contains(tbCabinCode.Text.Trim()));

            this.AspNetPager1.RecordCount = sql.Count();
            this.rptDetr.DataSource = sql.Skip((AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize)
                .OrderByDescending(l=>l.Discount).OrderBy(l => l.AirWayCode)
                //.Select(l => new { l.UserModel, l.CompanyName })
                ;
            ;
            this.rptDetr.DataBind();

            this.AspNetPager1.CustomInfoHTML = string.Format("当前第{0}/{1}页 共{2}条记录 每页{3}条", new object[] { this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageCount, this.AspNetPager1.RecordCount, this.AspNetPager1.PageSize });
        }

        protected void btnQuery_Click(object sender, EventArgs e) {
            AspNetPager1.CurrentPageIndex = 1;
            QueryData();
        }

        protected void rptDetr_OnItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) {
                LinkButton lnkEdit = e.Item.FindControl(@"lnkEdit") as LinkButton;
                LinkButton lnkDelete = e.Item.FindControl(@"lnkDelete") as LinkButton;

                PnrCabin model = e.Item.DataItem as PnrCabin ;
                BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, lnkEdit.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-edit'  
}});  
", lnkEdit.ClientID), true);

                BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, lnkDelete.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-cancel'  
}});  
", lnkDelete.ClientID), true);

                lnkDelete.OnClientClick = string.Format(@"return false;", @"");


                //,Get<object>(dataItem,@"UserModel.UserId")
                lnkEdit.OnClientClick = string.Format(@" 
$.jBox(""iframe:CabinEditor.aspx?ID={0}"", {{
    title: ""舱位信息"",
    width: 450,
    height:270,
    buttons: {{ }},
    closed: function () {{ 
        $('#{1}').click();
    }} /* 窗口关闭后执行的函数 */
}});
return false;", model.Id, btnQuery.ClientID);
            }
        }

        protected void AspNetPager1_PageChanging(object sender, Wuqi.Webdiyer.PageChangingEventArgs e) { 
            
        }
    }
}
